package com.edu.control.deploy;

import com.edu.control.deploy.constant.DeployStatus;
import com.edu.control.deploy.constant.PodStatus;
import com.edu.control.k8s.KubernetesApi;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1ContainerState;
import io.kubernetes.client.openapi.models.V1ContainerStatus;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.openapi.models.V1PodStatus;
import org.slf4j.Logger;

import java.util.List;

public abstract class CommonMethod {

    public V1PodList getV1PodList(KubernetesApi kubernetesApi) throws ApiException {
        V1PodList v1PodList = kubernetesApi.listNamespacedPod(getNamespace(),
                null,
                false,
                null,
                null,
                getLabels(),
                null,
                null,
                null,
                false);

        return v1PodList;
    }

    public void store(DeployStorage deployStorage, PodInfo podInfo, DeployStatus deployStatus, PodResult podResult, final Logger LOGGER) {
        DeployResult deployResult = DeployResult.newDeployResult(deployStatus);
        deployResult.setPodResult(podResult);
        deployStorage.store(podInfo.getFunctionName(), deployResult);
        LOGGER.info("pod result：{}", podResult != null ? podResult.toString() : "");
    }

    public void loopV1Pods(List<V1Pod> items, PodResult podResult) {
        for (V1Pod item : items) {
            V1PodStatus status = item.getStatus();
            switch (status.getPhase()) {
                case PodStatus.PENDING: {
                    podResult.pendingNum++;
                    List<V1ContainerStatus> containerStatuses = status.getContainerStatuses();
                    if (!containerStatuses.isEmpty()) {
                        for (V1ContainerStatus containerStatus : containerStatuses) {
                            V1ContainerState state = containerStatus.getState();
                            if (state != null && state.getWaiting() != null) {
                                String reason = state.getWaiting().getReason();
                                podResult.addPendIngReason(reason);
                            }
                        }
                    }
                    break;
                }
                case PodStatus.RUNNING: {
                    podResult.runningNum++;
                    break;
                }
                case PodStatus.SUCCEEDED: {
                    podResult.succeededNum++;
                    break;
                }
                case PodStatus.FAILED: {
                    podResult.failedNum++;
                    podResult.addFailedReason(status.getReason());
                    break;
                }
                case PodStatus.UNKNOWN: {
                    podResult.unknownNum++;
                    podResult.addUnknownReason(status.getReason());
                    break;
                }
            }
        }
    }

    public abstract String getNamespace();

    public abstract String getLabels();

}
